package com.yenlien.traceyun.plant.controller;

import com.yenlien.traceyun.plant.dao.CompanyDao;
import com.yenlien.traceyun.plant.entity.Article;
import com.yenlien.traceyun.plant.entity.FruitBanner;
import com.yenlien.traceyun.plant.entity.Company;
import com.yenlien.traceyun.plant.service.CMSService;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/cms")
@Api(tags = "果园CMS")
public class CMSController extends BaseController {

    @Autowired
    private CMSService cmsService;
    @Autowired
    private CompanyDao companyDao;


    @PostMapping(value = "/saveBanner")
    @ApiOperation(value = "批量保存Banner", notes = "img为图片上传后的名字(图片先上传)。有Id则代表修改，没有则是添加")
    public Object save(@RequestBody FruitBanner fruitBanner)  {
        return success(cmsService.saveBanner(fruitBanner));
    }


    @PostMapping(value = "/saveArticle")
    @ApiOperation(value = "保存文章", notes = "img为图片上传后的名字(图片先上传)。有Id则代表修改，没有则是添加")
    public Object saveArticle(@RequestBody Article article)  {
        return success(cmsService.saveArticle(article));
    }


    @PostMapping(value = "/saveComp")
    @ApiOperation(value = "保存名企名品", notes = "原有数据原封不动传递")
    public Object saveComp(@RequestBody Company company)  {
        return success(companyDao.save(company));
    }








    @GetMapping(value = "/state")
    @ApiOperation(value = "修改文章状态", notes = "参数：id=文章ID, state= 1(未发布), 2(已发布)")
    public Object state(@RequestParam String id, @RequestParam(defaultValue = "2") String state)  {
        return success(cmsService.state(id, state));
    }


    @GetMapping(value = "/findArticleDown")
    @ApiOperation(value = "获取已发布的文章下拉列表", notes = "返回值包含ID和名称, 可选参数: companyId (未传则使用当前账号所属company)")
    public Object findArticleDown(String companyId)  {
        return success(cmsService.findArticleDown(companyId));
    }


    @GetMapping(value = "/findArticleById")
    @ApiOperation(value = "根据ID查看文章", notes = "参数： id")
    public Object findArticleById(String id)  {
        return success(cmsService.findArticleById(id));
    }

    @PostMapping(value = "/findBannerList")
    @ApiOperation(value = "分页查询Banner", notes = "可选参数: type")
    public Object findBannerList(@RequestBody FruitBanner fruitBanner)  {
        return success(cmsService.findBannerList(fruitBanner));
    }

    @PostMapping(value = "/findContentList")
    @ApiOperation(value = "分页查询文章", notes = "可选参数: type, state, homeShow, hotspot, publisher")
    public Object findContentList(@RequestBody Article article)  {
        return success(cmsService.findContentList(article));
    }

















}
